package com.mybatis.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisCacheClear {
    public static void main(String[] args) throws IOException {
        // 读取 MyBatis 配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取 SqlSession
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            // 清除 MyBatis 二级缓存
            sqlSession.getConfiguration().getCache("com.example.mapper.UserMapper").clear();
            System.out.println("MyBatis 二级缓存已清除！");
        }
    }
}
